package com.opensource.afuluo.valve.integration.file;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.sax.Excel07SaxReader;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.opensource.afuluo.annotation.FileField;
import com.opensource.afuluo.valve.integration.model.CommonDO;
import com.opensource.afuluo.valve.integration.model.TestTableDO;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.regex.Pattern;

public class ExcelProxyUtils {

    private static Pattern LINE_PATTERN = Pattern.compile("_(\\w)");

    public static ExcelReader getReader(String fileName) {
        return ExcelUtil.getReader(ResourceUtil.getStream(fileName));
    }

    public static ExcelReader getReader(String fileName, String sheet) {
        return ExcelUtil.getReader(ResourceUtil.getStream(fileName), sheet);
    }

    public static <T> List<T> readAsBean(Class<T> clazz, String fileName, String sheet) {
        ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream(fileName), sheet);
        if (null == reader) {
            // 异常处理
        }
        return reader.readAll(clazz);
    }

    public static <T> List<T> readBeanOffset(Class<T> clazz, String fileName, String sheet, int cursor, int offset) {
        ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream(fileName), sheet);
        if (null == reader) {
            // 异常处理
        }
        return reader.read(cursor, offset, clazz);
    }

    public static void readWithinYield(String fileName, String sheet, RowHandler yieldHandler) {
        Excel07SaxReader reader = new Excel07SaxReader(yieldHandler);
        reader.read(ResourceUtil.getStream(fileName), sheet);
    }

    public static ExcelWriter getWriter(String fileName) {
        return ExcelUtil.getWriter(FileUtil.file(fileName));
    }

    public static ExcelWriter getWriter(String fileName, String sheet) {
        return ExcelUtil.getWriter(FileUtil.file(fileName), sheet);
    }

    public static void writeExcel(String fileName, String sheet, List<? extends CommonDO> fileDOS) {
        ExcelWriter writer = ExcelUtil.getWriter(FileUtil.file(fileName), sheet);
        if (CollectionUtils.isEmpty(fileDOS)) {
            // 处理异常
        }
        Field[] fields = fileDOS.get(0).getClass().getDeclaredFields();
        for (Field field : fields) {
            FileField fileField = field.getAnnotation(FileField.class);
            if (null != fileField) {
                writer.addHeaderAlias(field.getName(), fileField.value());
            }
        }

        writer.merge(fields.length - 1, "自动生成结果");
        writer.write(fileDOS, true);
        writer.flush();
        writer.close();
    }

    public static void main(String[] args) {
        //ExcelProxyUtils.readWithinYield("/excel/test_table.xls", "test_table", new RowHandler() {
        //    @Override
        //    public void handle(int sheetIndex, long rowIndex, List<Object> rowList) {
        //        if (!CollectionUtils.isEmpty(rowList)) {
        //            ExcelProxyUtils.w
        //        }
        //    }
        //});
        List<TestTableDO> rows = ExcelProxyUtils.readAsBean(TestTableDO.class, "excels/test_table.xlsx", "test_table");
        ExcelProxyUtils.writeExcel("excels/test_table.xlsx", "test_table_ca", rows);
    }

}
